var cluster = require('cluster');
var http = require('http');
var Client = require('mysql').Client,
	client = new Client();
var numCPUs = require('os').cpus().length;
	
client.host = 'localhost';
client.user = 'root'
client.password = 'root';
client.database = 'test';
client.port = '3306';

if(cluster.isMaster){
	for (var i = 0; i < numCPUs; i++){
		cluster.fork();
	}
	
	cluster.on('exit', function(worker, code, signal){
		console.log('worker ' + worker.process.pid + ' died');
	});
} else {
	http.createServer(function(req,res){
		res.writeHead(200, {'Content-Type':'text/plain; charset=UTF-8'});
		client.query('select * from testtable where id > 1 and id < 7', function(err, results, fields){
			if(err){
				throw err;
			}
			res.write(fields+"\n");
			for (var i in results){
				var data = JSON.stringify(results[i]);
				res.write(data+"\n");
			}
			res.end();
		});
	}).listen(12345);

	console.log("server is started");
}